// StpLibrary - by Steve T. Pham
// http://www.geocities.com/stevetpham

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SteveTPham.QueryAnything.DataAccess;
using SteveTPham.QueryAnything.DataSetTools;

namespace SteveTPham.QueryAnything.Controls.DatabaseUC
{
	public partial class QueryResultsViewer : UserControl
	{
		private QueryResult results;

		public QueryResultsViewer()
		{
			InitializeComponent();
		}

		public QueryResultsViewer(QueryResult result)
			: this()
		{
			Results = result;
		}

		public QueryResult Results
		{
			get
			{
				return this.results;
			}
			set
			{
				this.results = value;
				AssignControls();
			}
		}

		private void AssignControls()
		{
			if (results == null)
			{
				dgDataResults.DataSource = null;
				txtTextResults.Text = string.Empty;
				return;
			}

			lblStatement.Text = results.Statement == null ? string.Empty : results.Statement.ToString();
			dgDataResults.DataSource = results.DataResult;

			txtTextResults.Text = string.Format("{0}\r\n{1}\r\n{2}",
				results.ErrorText,
				results.TextResult,
				results.RowAffected == -1 ? "" : "(" + results.RowAffected + " row(s) affected)");

			if (results.DataResult != null && string.IsNullOrEmpty(results.ErrorText))
				tabControl1.SelectedTab = tabDataResults;
			else
				tabControl1.SelectedTab = tabTextResults;
		}
	}
}
